﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core.Base;
using System.ComponentModel;
using Base;
using System.Drawing;
using Core.Component;
using Core;
using System.IO;
using System.Drawing.Imaging;

namespace Components
{
    [System.ComponentModel.DefaultBindingProperty("ImageValue")]
    public class SGVPictureBox : PictureBox, IBindingControl, ISGVControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private byte[] _imageBytes = null;
        public byte[] ImageValue
        {
            get 
            {
                if (Image != null)
                {
                    try
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            Bitmap bmp = new Bitmap(Image);
                            bmp.Save(ms, Image.RawFormat);
                            byte[] temp = ms.ToArray();
                            ms.Dispose();
                            return temp;
                        }
                        
                    }
                    catch (Exception ex)
                    {
                    }
                    
                }
                return null;
            }
            set 
            { 
                _imageBytes = value;
                try
                {
                    Image = ByteToImage(_imageBytes);
                }
                catch (Exception ex)
                {
                    Image = null;
                }
                

                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ImageValue"));
            }
        }

        public ToolStripMenuItem mnuTakePicture;
        public SGVPictureBox()
            :base()
        {
            
        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        public static Bitmap ByteToImage(byte[] blob)
        {
            MemoryStream mStream = new MemoryStream();
            byte[] pData = blob;
            mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
            Bitmap bm = new Bitmap(mStream, false);
            mStream.Dispose();
            return bm;

        }
        void mnuTakePicture_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDlg = new OpenFileDialog();
                openFileDlg.Multiselect = false;
                openFileDlg.ShowDialog();

                String strFile = openFileDlg.FileName;

                Image img = Bitmap.FromFile(strFile);
                MemoryStream ms = new MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                ImageValue = ms.ToArray();
            }
            catch (Exception ex)
            {
            }
            
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        #region IBindingControl
        BaseModule module;
        BaseEntity entity;
        String _bindingPropertyName = "ImageValue";

        String _bindingTable;
        String _bindingColumn;
        String _dataSourceTable;
        String _dataSourceDisplayColumn;
        String _dataSourceKeyColumn;
        String _dataSourceColumns;
        String _sGVShortcutString;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String SGVShortcutString
        {
            get { return _sGVShortcutString; }
            set { _sGVShortcutString = value; }
        }
        String _sGVTag;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get
            {
                return _sGVTag;
            }
            set
            {
                _sGVTag = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingPropertyName
        {
            get
            {
                return _bindingPropertyName;
            }
            set
            {
                _bindingPropertyName = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingTable
        {
            get
            {
                return _bindingTable;
            }
            set
            {
                _bindingTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingColumn
        {
            get
            {
                return _bindingColumn;
            }
            set
            {
                _bindingColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceTable
        {
            get
            {
                return _dataSourceTable;
            }
            set
            {
                _dataSourceTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceDisplayColumn
        {
            get
            {
                return _dataSourceDisplayColumn;
            }
            set
            {
                _dataSourceDisplayColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceKeyColumn
        {
            get
            {
                return _dataSourceKeyColumn;
            }
            set
            {
                _dataSourceKeyColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String DataSourceColumns
        {
            get
            {
                return _dataSourceColumns;
            }
            set
            {
                _dataSourceColumns = value;
            }
        }

        public void RegisterModule(Core.Base.BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        #endregion

        public void Init()
        {
            this.ContextMenuStrip = new ContextMenuStrip();
            mnuTakePicture = new ToolStripMenuItem("Chọn ảnh");
            this.ContextMenuStrip.Items.Add(mnuTakePicture);
            this.SizeMode = PictureBoxSizeMode.StretchImage;
            mnuTakePicture.Click += new EventHandler(mnuTakePicture_Click);
            this.CausesValidation = false;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        }

        public void UpdateFormat()
        {
            
        }

        public UIComponent Serialize()
        {
            UIComponent component = new UIComponent();
            SGVControlHelper.CreateUIComponentFromControl(component, this);

            if (component != null)
            {
                component.Value = this.Text;
            }
            return component;
        }

        public void Deserialize(UIComponent component)
        {
            throw new NotImplementedException();
        }


        UIComponent ISGVControl.component
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string ISGVControl.SGVTag
        {
            set;
            get;
        }

        string ISGVControl.SGVShortcutString
        {
            set;
            get;
        }

        void ISGVControl.RegisterModule(BaseModule module)
        {
            throw new NotImplementedException();
        }

        void ISGVControl.UpdateFormat()
        {
            throw new NotImplementedException();
        }

        void ISGVControl.Init()
        {
            throw new NotImplementedException();
        }
    }
}
